﻿/***
 * 
 *    Title: "ZGUIFW"UI框架项目
 *           主题： UI窗体的父类      
 *    Description: 
 *           功能： 定义所有的UI窗体父类
 *           定义四个生命周期：
 *              1.Display显示
 *              2.hiding 隐藏状
 *              3.ReDisplay再显示
 *              4.Freeze冻结
 *    Date: 2018
 *    Version: 0.1版本
 */
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ZGUIFW
{
    public class BaseUIForm : MonoBehaviour
    {
		public UIType CurrentType = new UIType();

        /// <summary>
        /// 显示状态
        /// </summary>
        public virtual void Display()
        {
			Reset();
            this.gameObject.SetActive(true);
			if(this.CurrentType.UiFormType == UIFormType.PopUP)
			{
				UIMaskMgr.GetInstance().SetMaskPanel(this.gameObject, this.CurrentType.UiFormLucenyType);
			}
        }
        /// <summary>
        /// 隐藏状态
        /// </summary>
        public virtual void Hiding()
        {
            this.gameObject.SetActive(false);
			if (this.CurrentType.UiFormType == UIFormType.PopUP)
            {
				UIMaskMgr.GetInstance().CancelMaskPanel();
            }
        }
        /// <summary>
        /// 重新显示状态
        /// </summary>
        public virtual void ReDisplay()
        {
            this.gameObject.SetActive(true);
			if (this.CurrentType.UiFormType == UIFormType.PopUP)
            {
				UIMaskMgr.GetInstance().SetMaskPanel(this.gameObject, this.CurrentType.UiFormLucenyType);
            }
        }
        /// <summary>
        /// 冻结状态
        /// </summary>
        public virtual void Freeze()
        {
            this.gameObject.SetActive(true);
        }
        public virtual void Reset()
        {
            gameObject.transform.localScale = Vector3.one;
        }
        /// <summary>
        /// Rigisters the button event.
        /// </summary>
        /// <param name="name">Name.</param>
        /// <param name="delHandle">Del handle.</param>
        protected void RigisterBtnEvent(string name, EventTriggerListener.VoidDelegate delHandle)
        {
            GameObject btn = UnityHelper.FindTheChildNode(gameObject, name).gameObject;
            if (btn == null) return;
            EventTriggerListener.Get(btn).onClick = delHandle;
        }
        /// <summary>
        /// Opens the UIForm.
        /// </summary>
        /// <param name="uiFormName">User interface form name.</param>
		protected void OpenUIForm(string uiFormName)
        {
            UIManager.GetInstance().ShowUIForms(uiFormName);
        }

        protected void CloseUIForm()
        {
			string[] names = this.name.Split('(');
			UIManager.GetInstance().CloseUIForms(names[0]);
        }
        /// <summary>
        /// Sends the mssage.
        /// </summary>
        /// <param name="msgType">Message type.</param>
        /// <param name="msgName">Message name.</param>
        /// <param name="msgValue">Message value.</param>
		protected void SendMssage(string msgType, string msgName, object msgValue)
		{
			MessageCenter.SendMsg(msgType, new KeyValueMsg(msgName, msgValue));
		}
        /// <summary>
        /// Receives the message.
        /// </summary>
        /// <param name="msgType">Message type.</param>
        /// <param name="handler">Handler.</param>
		protected void ReceiveMessage(string msgType, MessageCenter.DelMessageDelivery handler)
		{
			MessageCenter.AddMsgListener(msgType, handler);
		}

		protected string ShowText(string key)
		{
			return LanguageMgr.GetInstance().ShowText(key);
		}
    }
}

